package dws_topic

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
	"server/utils"
)

type DwsTopicApi struct {
}

var dwsTopicService = DwsTopicService{}


func (dwsTopicApi *DwsTopicApi) CreateDwsTopic(c *gin.Context) {
	var dwsTopic DwsTopic
	err := c.ShouldBindJSON(&dwsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTopic.CreatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"Name":        {utils.NotEmpty()},
		"Code":        {utils.NotEmpty()},
		"Applicant":   {utils.NotEmpty()},
		"ApplyReason": {utils.NotEmpty()},
		"ApplyTeam":   {utils.NotEmpty()},
		"Contract":    {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsTopic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsTopicService.CreateDwsTopic(&dwsTopic); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) DeleteDwsTopic(c *gin.Context) {
	var dwsTopic DwsTopic
	err := c.ShouldBindJSON(&dwsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTopic.DeletedBy = utils.GetUserID(c)
	if err := dwsTopicService.DeleteDwsTopic(dwsTopic); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) DeleteDwsTopicByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	deletedBy := utils.GetUserID(c)
	if err := dwsTopicService.DeleteDwsTopicByIds(IDS, deletedBy); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) UpdateDwsTopic(c *gin.Context) {
	var dwsTopic DwsTopic
	err := c.ShouldBindJSON(&dwsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	dwsTopic.UpdatedBy = utils.GetUserID(c)
	verify := utils.Rules{
		"Name":        {utils.NotEmpty()},
		"Code":        {utils.NotEmpty()},
		"Applicant":   {utils.NotEmpty()},
		"ApplyReason": {utils.NotEmpty()},
		"ApplyTeam":   {utils.NotEmpty()},
		"Contract":    {utils.NotEmpty()},
	}
	if err := utils.Verify(dwsTopic, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := dwsTopicService.UpdateDwsTopic(dwsTopic); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) FindDwsTopic(c *gin.Context) {
	var dwsTopic DwsTopic
	err := c.ShouldBindQuery(&dwsTopic)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if redwsTopic, err := dwsTopicService.GetDwsTopicById(dwsTopic.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"redwsTopic": redwsTopic}, c)
	}
}


func (dwsTopicApi *DwsTopicApi) GetDwsTopicList(c *gin.Context) {
	var pageInfo DwsTopicSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := dwsTopicService.GetDwsTopicInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) GetAllDwsTopics(c *gin.Context) {
	dwsTopic, err := dwsTopicService.GetAllDwsTopics()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsTopic": dwsTopic}, "获取成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) GetDwsTopic(c *gin.Context) {
	dwsTopic, err := dwsTopicService.GetDwsTopic()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"dwsTopic": dwsTopic}, "获取成功", c)
	}
}


func (dwsTopicApi *DwsTopicApi) InitDwsTopic(c *gin.Context) {
	response.FailWithMessage("获取初始化数据失败", c)
}
